import Vue from 'vue'
import  VueRouter  from "vue-router"

import store from '@/store'
//让router-view组件、router-link组件编程全局组件
Vue.use(VueRouter)


let router = new VueRouter({
	routes: [
		{
			path: '/login',
			name:'Login',
			component: ()=>import('@/views/login.vue'),
		},
		{
			path: '/',
			name:'Home',
			component: ()=>import('@/views/home.vue'),
			children:[
				{
					path: '/user',
					component: ()=>import('@/views/user'),
				},
				{
					path: '/customer',
					component: ()=>import('@/views/customer'),
				},
				{
					path: '/role',
					component: ()=>import('@/views/role'),
				},
				{
					path: '/role_grant',
					component: ()=>import('@/views/role/grant'),
				},{
					path: '/user_grant',
					component: ()=>import('@/views/user/grant'),
				},{
					path: '/doctor_home',
					component: ()=>import('@/views/doctor'),
				},{
					path: '/menu',
					component: ()=>import('@/views/menu'),
				},
				{
					path: '/test/teacher',
					component: ()=>import('@/views/test/teacher'),
				} ,{
					path: '/test/student',
					component: ()=>import('@/views/test/student'),
				},
				
			]
		},
		
	
	]
})


//白名单
let  whitelist = [
	'/login'
]


router.beforeEach((to,from,next)=>{
	// console.log("to:",to)
	// console.log("from",from)
	// console.log("next:",next);
	let token = store.getters.getToken;
	//如果登录状态不存在，需要跳转到登录页
	let toPath = to.path; //不在白名单中
	if(!token  &&  whitelist.indexOf(toPath) == -1){
		next({name:'Login'})
	}else{//登录的状态，继续跳转页面
		next()
	}
	
	// next()
	
})


export default router